<?php
/**
 * 实现 Laravel 视图返回方法加载 twig 视图。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace Lib\Twig;

use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Foundation\Application;

class TwigFactory implements FactoryContract
{
    /*
    * Twig environment
    *
    * @var Twig_Environment
    * 
    */
    private $twig;

    public function __construct(Application $app)
    {
        $this->twig = $app['twig'];
    }

    public function exists($view)
    {
        return $this->twig->getLoader()->exists($view);
    }

    public function file($path, $data = [], $mergeData = [])
    {
        // or maybe use the String loader
        if (!file_exists($path)) {
            return false;
        }

        $filePath = dirname($path);
        $fileName = basename($path);
        
        $this->twig->getLoader()->addPath($filePath);

        return new TwigView($this, $fileName, $data);
    }

    public function make($view, $data = [], $mergeData = [])
    {
        $data = array_merge($mergeData, $data);
        
        return new TwigView($this, $view, $data);
    }

    public function share($key, $value = null)
    {
        $this->twig->addGlobal($key, $value);
    }

    public function render($view, $data)
    {
        return $this->twig->render($view, $data);
    }

    public function composer($views, $callback, $priority = null)
    {
    }

    public function creator($views, $callback)
    {
    }

    public function addNamespace($namespace, $hints)
    {
    }
}
